<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-18
 * Time: 18:26
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\Coupon as CouponModel;
use app\api\model\CouponUse;
use app\api\model\GoodsOrder;
use app\api\service\coupon\CouponOrderSer;
use app\api\service\coupon\CouponSer;
use app\api\service\Token;
use app\api\model\StoreOrder;

class Coupon extends BaseController
{
    /**
     * 获取可领取优惠劵
     */
    public function getCouponList()
    {
        $uid = Token::getCurrentUid();
        $list = CouponUse::where('user_id',$uid)->select()->toArray();
        $ids = array_column($list,'coupon_id');
        $time = time();
        $where[] = ['start_time','elt',$time];
        $where[] = ['end_time','egt',$time];
        $where[] = ['id','not in',$ids];
        return CouponModel::where($where)->order('id desc')->select()->each(function ($item){
            $item['start_date'] = date('Y-m-d',$item['start_time']);
            $item['end_date'] = date('Y-m-d',$item['end_time']);
        })->toArray();
    }

    /**
     * 获取用户优惠券
     */
    public function getCouponListByUser($status)
    {
        $uid = Token::getCurrentUid();
        if($status == 1) {
            return CouponUse::hasWhere('coupon')->where('user_id',$uid)
                ->with('coupon')
                ->where('coupon_use.status',$status)
                ->order('coupon_use.id desc')
                ->select()->each(function ($item){
                    $item['coupon']['start_date'] = date('Y-m-d',$item['coupon']['start_time']);
                    $item['coupon']['end_date'] = date('Y-m-d',$item['coupon']['end_time']);
                })->toArray();
        } else {
            return CouponUse::hasWhere('coupon')->where('user_id',$uid)
                ->with('coupon')
                ->where('coupon_use.status','neq',1)
                ->order('coupon_use.id desc')
                ->select()->each(function ($item){
                    $item['coupon']['start_date'] = date('Y-m-d',$item['coupon']['start_time']);
                    $item['coupon']['end_date'] = date('Y-m-d',$item['coupon']['end_time']);
                })->toArray();
        }
    }

    /**
     * 领取优惠券
     */
    public function setCoupon($coupon_id)
    {
        $couponSer = new CouponSer($coupon_id);
        return $couponSer->entrance();
    }

    /**
     * 获取用户可使用的优惠券
     */
    public function getCouponListByOrder($type,$order_id)
    {
        $uid = Token::getCurrentUid();
        if($type == 1) {
            $order = StoreOrder::get($order_id);
        } else {
            $order = GoodsOrder::get($order_id);
        }
        return CouponUse::hasWhere('coupon',function ($query) use($order){
            $query->where('start_time','<=',time())
                ->where('end_time','>=',time())
                ->where('money','<=',$order['price']);
        })->with('coupon')
            ->where('user_id',$uid)
            ->where('coupon_use.status','=',1)
            ->order('coupon_use.id desc')
            ->select()->toArray();
    }

    /**
     * 订单使用优惠券
     */
    public function changeCouponByOrder($type,$coupon_use_id,$order_id)
    {
        $couponOrderSer = new CouponOrderSer($type,$coupon_use_id,$order_id);
        return $couponOrderSer->entrance();
    }
}